//
//  PhotoGalleryViewController.m
//  AirConditioner
//
//  Created by Sean Wang on 12-5-3.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "PhotoGalleryViewController.h"

static NSUInteger kNumberOfPages = 6;

static NSString *NameKey = @"nameKey";
static NSString *ImageKey = @"imageKey";

@interface PhotoGalleryViewController ()

@end

@implementation PhotoGalleryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"content_iPhone" ofType:@"plist"];
        contentList = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *imageViews = [[NSMutableArray alloc] init];
        for (unsigned i = 0; i < kNumberOfPages; i++)
        {
            [imageViews addObject:[NSNull null]];
        }
        viewControllers = imageViews;
        
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //self.tabBarController.tabBar.hidden = YES;
    scrollView = (UIScrollView *)self.view;
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollsToTop = NO;
    scrollView.delegate = self;
    
    // pages are created on demand
    // load the visible page
    // load the page on either side to avoid flashes when the user starts scrolling
    //
    [self loadScrollViewWithPage:0];
    [self loadScrollViewWithPage:1];

}

- (void)loadScrollViewWithPage:(int)page
{
    if (page < 0)
        return;
    if (page >= kNumberOfPages)
        return;
    
    // replace the placeholder if necessary
    UIImageView *imageView = [viewControllers objectAtIndex:page];
    if ((NSNull *)imageView == [NSNull null])
    {    
        NSDictionary *numberItem = [contentList objectAtIndex:page];
        UIImage *tempImage = [UIImage imageNamed:[numberItem valueForKey:ImageKey]];
        imageView = [[UIImageView alloc] initWithImage:tempImage];
        [viewControllers replaceObjectAtIndex:page withObject:imageView];
    }
    
    // add the controller's view to the scroll view
    if (imageView.superview == nil)
    {
        CGRect frame = scrollView.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        imageView.frame = frame;
        
        //NSDictionary *numberItem = [contentList objectAtIndex:page];
        //imageView.image = [UIImage imageNamed:@"air.png"];
        [scrollView addSubview:imageView];
        
        
        //controller.numberImage.image = [UIImage imageNamed:[numberItem valueForKey:ImageKey]];
        //controller.numberTitle.text = [numberItem valueForKey:NameKey];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    //NSLog(@"scrollViewDidScroll");
	
    // Switch the indicator when more than 50% of the previous/next page is visible
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    
    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];
    //NSLog(@"scrollViewDidScroll end");
    // A possible optimization would be to unload the views+controllers which are no longer visible
}

// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    
}

// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
}


//- (void)viewWillAppear:(BOOL)animated
//{
//    CGRect frame = self.view.frame;
//    frame.size.height = 416;
//    self.view.frame = frame;
//}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
